$(document).ready(function() {
    // $("form").submit(function() {
    // 	return false;
    // });
    var VAL = {
        NORMAL : "0000000",
        EVT_USER : $("#userId").text(),
        T_FSPFUTACTION : "FSPFUTACTION",
        T_FSPSMPRLN : "FSPSMPRLN",
        T_FBPMDLDEF	: "FBPMDLDEF",
        T_FBPBISDAT : "FBPBISDAT",
        T_FDPAPLYWO : "FDPAPLYWO",
        T_FBPBISPTH : "FBPBISPTH",
        T_FIPINQSHT : "FIPINQSHT",
        T_FBPBISMTR : "FBPBISMTR",
        T_FBPBISBOM : "FBPBISBOM",
        T_FBPBISTOL : "FBPBISTOL",
        T_FSPSMPDEF : "FSPSMPDEF",

    };
    var domObj = {
        $window : $(window),
        $samp_tpl_id : $("#samp_tpl_id_fk"),   //模版ID
        $mdl_id_fk : $("#mdl_id_fk"),             //产品代码
        $tool_id : $("#tool_id_fk"),
        $path_id_fk : $("#path_id_fk"),           //工艺路线
        $path_ver_fk : $("#path_ver_fk"),        //工艺版本
        $ope_no_fk : $("#ope_no_fk"),             //操作代码
        $ope_id_fk     : $("#ope_id_fk"),        //制程代码
        $ope_ver_fk     : $("#ope_ver_fk"),        //制程代码
        $samp_qty  : $("#samp_qty"),                //取样频率
        $rsv_batch_qty : $("#rsv_batch_qty"),       //取样批数
        $crt_note : $("#crt_note"),

        grids : {
            $prdInfoGrd : $("#prdInfoGrd"),
            $boxInfoGrd : $("#boxInfoGrd")
        },

        buttons : {
            $query_btn : $("#query_btn"),          //获取预约列表
            $delete_btn : $("#delete_btn"),          //删除
            $addRsn_btn : $("#addRsn_btn"),        //登记
            $export_btn : $("#delRsn_btn"),         //导出列表
            $acti_free : $("#acti_free"),           //激活锁定
            $priority_btn : $("#priority_btn"),     //优先级提升
            $importSte_btn : $("#importSte_btn"),   //导入模版下载
            $import_btn : $("#import_btn"),         //导入



        },

    };
    function initPrdInfoGrd() {
        domObj.grids.$prdInfoGrd.jqGrid({
            url : "",
            datatype : "local",
            mtype : "POST",
            height : 250,
            width : $("#prdInfoGrdDiv").width ,
            autowidth : true,
            shrinkToFit : true,
            resizable : true,
            loadonce : true,
            viewrecords : true,
            rownumbers : true,
            rownumWidth : 30,
            rowNum : 20,
            multiselect: true,
            multiboxonly:true,
            emptyrecords : true,
            colModel : [ {
                name : 'valid_flg',
                index : 'valid_flg',
                label : '激活标识',
                width : 80
            }, {
                name : 'ope_id_fk',
                index : 'ope_id_fk',
                label : '制程代码',
                width : 80
            }, {
                name : 'ope_ver_fk',
                index : 'ope_ver_fk',
                label : '制程版本',
                width : 80
            }, {
                name : 'tool_id_fk',
                index : 'tool_id_fk',
                label : '设备代码',
                width : 80
            }, {
                name : 'samp_prty',
                index : 'samp_prty',
                label : '优先级',
                width : 80
            },
                {
                    name : 'mdl_id_fk',
                    index : 'mdl_id_fk',
                    label : '产品代码',
                    width : 80
                },
                {
                    name : 'path_id_fk',
                    index : 'path_id_fk',
                    label : '工艺路线',
                    width : 80
                },
                {
                    name : 'path_ver_fk',
                    index : 'path_ver_fk',
                    label : '工艺版本',
                    width : 80
                },
                {
                    name : 'ope_no_fk',
                    index : 'ope_no_fk',
                    label : '操作代码',
                    width : 80
                },
                {
                    name : 'samp_tpl_id_fk',
                    index : 'samp_tpl_id_fk',
                    label : '模版ID',
                    width : 80
                },
                {
                    name : 'samp_qty',
                    index : 'samp_qty',
                    label : '间隔计数',
                    width : 80
                },
                {
                    name : 'rsv_batch_qty',
                    index : 'rsv_batch_qty',
                    label : '取样批次',
                    width : 80
                },
                {
                    name : 'pass_qty',
                    index : 'pass_qty',
                    label : '过账计数',
                    width : 80
                },
                {
                    name : 'samp_qty',
                    index : 'samp_qty',
                    label : '取样计数',
                    width : 80
                },
                {
                    name : 'crt_usr',
                    index : 'crt_usr',
                    label : '创建人',
                    width : 80
                },
                {
                    name : 'crt_timestamp',
                    index : 'crt_timestamp',
                    label : '创建时间',
                    width : 80
                },
                {
                    name : 'crt_note',
                    index : 'crt_note',
                    label : '备注',
                    width : 80
                },
                {
                    name : 'samp_seq_id',
                    index : 'samp_seq_id',
                    label : '流水码',
                    width : 80,
                    hidden: true
                }
            ],
            pager : "prdInfoPg",
            onSelectRow:function(rowid) {
                setGridInfo("",domObj.grids.$boxInfoGrd);
                var rowDatas = $("#prdInfoGrd").jqGrid('getRowData', rowid);
                var tplSeqId = rowDatas.tpl_seq_id;
                toolFun.findSlotList(tplSeqId);
            },

        });
    };

    var toolFun = {
        initPathVer : function () {
            var inObj,
                outObj,
                iaryA,
                iaryA = {
                    path_id: domObj.$path_id_fk.val(),
                    path_cate :"MAIN"
                }
            inObj = {
                trx_id: VAL.T_FBPBISPTH,
                action_flg: 'Q',
                iaryA: [iaryA]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.tbl_cnt_a > 0) {
                    var oaryA = outObj.oaryA;
                    for (var i = 0; i < oaryA.length; i++){
                        SelectDom.addSelect(domObj.$path_ver_fk,oaryA[i].path_ver,oaryA[i].path_ver);
                    }

                }
            }
        },
        initMdl : function () {
            var inObj,
                outObj,
                iary,
                iary = {};
            inObj = {
                trx_id: VAL.T_FBPMDLDEF,
                action_flg: 'Q',
                iary: [iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            SelectDom.addSelect(domObj.$mdl_id_fk," "," ");
            if (outObj != null) {
                if (outObj.rtn_code == VAL.NORMAL) {
                    if (outObj.tbl_cnt > 0) {
                        var oary = outObj.oary;
                        for (var i = 0; i < oary.length; i++) {
                            SelectDom.addSelect(domObj.$mdl_id_fk, oary[i].mdl_id, oary[i].mdl_id);
                        }
                    }
                }
            }
        },//初始化产品代码
        initSampTplId : function () {
            var inObj,
                outObj,
                iary;
            iary = {
                data_cate : "SMPT",
            };
            inObj = {
                trx_id: VAL.T_FBPBISDAT,
                action_flg: 'Q',
                iary: [iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj != null) {
                if (outObj.rtn_code == VAL.NORMAL) {
                    if (outObj.tbl_cnt > 0) {
                        var oary = outObj.oary;
                        for (var i = 0;i < oary.length;i++) {
                            SelectDom.addSelect(domObj.$samp_tpl_id,oary[i].data_ext,oary[i].data_ext);
                        }

                    }
                }
            }
        },
        initOpe_no : function () {
            var inObj,
                outObj,
                iary;
            var iaryA = {};
            var iaryB = {};
            if (domObj.$path_id_fk.val() != ""  && domObj.$path_ver_fk.val() != "") {
                iaryA.path_id    = domObj.$path_id_fk.val();
                iaryB.path_id_fk = domObj.$path_id_fk.val();
                iaryA.path_ver    = domObj.$path_ver_fk.val();
                iaryB.path_ver_fk = domObj.$path_ver_fk.val();
            } else {
                return false;
            }
            inObj = {
                trx_id: VAL.T_FBPBISPTH,
                action_flg: 'Q',
                iaryA      : [iaryA]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.tbl_cnt_b > 0) {
                    var oaryB = outObj.oaryB;
                    for (var i = 0;i < oaryB.length;i++) {
                        SelectDom.setSelect(domObj.$ope_no_fk,oaryB[i].cr_ope_no,oaryB[i].cr_ope_no);
                    }
                }
            }
        },
        initOpe_id : function () {
            var inObj,
                outObj,
                iary;
            var iaryB = {};
            if (domObj.$ope_no_fk != "") {
                iaryB.path_id_fk = domObj.$path_id_fk.val();
                iaryB.path_ver_ver = domObj.$path_ver_fk.val();
                iaryB.cr_ope_no = domObj.$ope_no_fk.val();
            } else {
                return false;
            }
            inObj = {
                trx_id: VAL.T_FBPBISPTH,
                action_flg: 'I',
                iaryB      : [iaryB]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.tbl_cnt_b > 0) {
                    var oaryB = outObj.oaryB;
                    SelectDom.addSelect(domObj.$ope_id_fk,oaryB[0].cr_ope_id_fk,oaryB[0].cr_ope_id_fk);
                    SelectDom.addSelect(domObj.$ope_ver_fk,oaryB[0].cr_ope_ver_fk,oaryB[0].cr_ope_ver_fk);

                }
            }
        },
        initTool : function () {
            var inObj, outObj;
            var item = "tool_id";
            var iary = {
                tool_run_mode : domObj.$ope_no_fk.val(),
                tool_stat     : 'RUN'
            };
            inObj = {
                trx_id      : VAL.T_FBPBISTOL,
                action_flg  : 'H',
                iary		 : [iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                var tbl_cnt = comParseInt( outObj.tbl_cnt_B);
                if( tbl_cnt == 1 ){
                    SelectDom.addSelect(domObj.$tool_id,outObj.oaryB[0][item],outObj.oaryB[0][item]);
                }else if(tbl_cnt>1){
                    for(var i=0;i<tbl_cnt;i++){
                        SelectDom.addSelect(domObj.$tool_id,outObj.oaryB[i][item],outObj.oaryB[i][item]);
                    }
                }
            }
        },
        showPath_id : function () {
            var inObj,
                outObj,
                iary,
                iary = {
                    mdl_id: domObj.$mdl_id_fk.val()
                }
            inObj = {
                trx_id: VAL.T_FBPMDLDEF,
                action_flg: 'Q',
                iary: [iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.tbl_cnt > 0) {
                    var oary = outObj.oary;
                    var pathIdFk = oary[0].path_id_fk;
                    toolFun.checkPathIdNoRes(pathIdFk,"MAIN");
                }
            }
        },
        checkPathIdNoRes  : function (path_id,path_cate) {
            var inObj,
                outObj,
                iaryA,
                iaryA = {
                    path_id: path_id,
                    path_cate :path_cate
                }
            inObj = {
                trx_id: VAL.T_FBPBISPTH,
                action_flg: 'Q',
                iaryA: [iaryA]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.tbl_cnt_a > 0) {
                    var oaryA = outObj.oaryA;
                    for (var i = 0; i < oaryA.length; i++){
                        SelectDom.setSelect(domObj.$path_id_fk,oaryA[i].path_id,oaryA[i].path_id);

                    }

                }
            }
        },
        checkSampPathId  : function () {
            var inObj,
                outObj,
                iaryA,
                iaryA = {
                    path_cate :"SAMP"
                }
            inObj = {
                trx_id: VAL.T_FBPBISPTH,
                action_flg: 'Q',
                iaryA: [iaryA]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.tbl_cnt_a > 0) {
                    var oaryA = outObj.oaryA;
                    for (var i = 0; i < oaryA.length; i++){
                        SelectDom.addSelect(domObj.$samp_path_id_fk,oaryA[i].path_id,oaryA[i].path_id);
                    }
                    toolFun.checkSampPathVer(oaryA[0].path_id);

                }
            }
        },
        checkSampPathVer  : function (path_id) {
            var inObj,
                outObj,
                iaryA,
                iaryA = {
                    path_id : path_id,
                    path_cate :"SAMP"
                }
            inObj = {
                trx_id: VAL.T_FBPBISPTH,
                action_flg: 'Q',
                iaryA: [iaryA]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.tbl_cnt_a > 0) {
                    var oaryA = outObj.oaryA;
                    SelectDom.addSelect(domObj.$samp_path_ver_fk,oaryA[0].path_ver,oaryA[0].path_ver);
                }
            }
        },
        findSlotList : function (tpl_seq_id) {
            var inObj,
                outObj,
                iary,
                iary = {
                    act_seq_id : tpl_seq_id
                },
                inObj = {
                    trx_id: VAL.T_FSPSMPRLN,
                    action_flg: 'S',
                    iary: [iary]
                };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.tbl_cnt > 0) {
                    setGridInfo(outObj.oaryS, domObj.grids.$boxInfoGrd);
                }
            }
        },

    };
    var btnFun = {
        getRelationList : function () {
            var inObj,
                outObj,
                iary,
                iary = {
                    samp_tpl_id_fk : $("#samp_tpl_id").val(),   //模版ID
                    mdl_id_fk : $("#mdl_id_fk").val(),             //产品代码
                    path_id_fk: $("#path_id_fk").val(),           //工艺路线
                    path_ver_fk : $("#path_ver_fk").val(),        //工艺版本
                    ope_no_fk : $("#ope_no_fk").val(),             //操作代码
                    ope_id_fk     : $("#ope_id_fk").val(),        //制程代码
                    ope_ver_fk     : $("#ope_ver_fk").val(),        //制程代码
                    tool_id_fk : $("#tool_id_fk").val(),            //设备代码
                },
                inObj = {
                    trx_id: VAL.T_FSPSMPDEF,
                    action_flg: 'Q',
                    iary: [iary]
                };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if (outObj.tbl_cnt > 0) {
                    setGridInfo(outObj.oary, domObj.grids.$prdInfoGrd);
                } else {
                    setGridInfo("", domObj.grids.$prdInfoGrd);
                    showErrorDialog(null,"没有数据");
                }
            }
        },
        active : function () {
            var rowId = domObj.grids.$prdInfoGrd
                .jqGrid("getGridParam", "selrow");
            var rowData = domObj.grids.$prdInfoGrd
                .jqGrid("getRowData", rowId);
            if(!rowData.hasOwnProperty("samp_seq_id")){
                showErrorDialog("", "请选择一条数据");
                return false;
            }
            var samp_seq_id = rowData.samp_seq_id;
            var inObj,
                outObj,
                iary,
                iary = {
                    samp_seq_id : samp_seq_id
                },
                inObj = {
                    trx_id: VAL.T_FSPSMPDEF,
                    action_flg: 'Y',
                    iary: [iary]
                };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                showSuccessDialog(outObj.rtn_mesg);
            }
        },
        downSample : function () {
            if ($("#downForm").length > 0) {
                $("#downForm").remove();
            }
            var str = '<form id="downForm" action="download.do" method="post">';
            str = str + '<input type="hidden" name="fileName" id= "fileName" />';
            str = str + "</form>";

            $(str).appendTo("body");
            $("#fileName").val("2920.xls");
            $("#downForm").submit();
        },
        export : function () {
            var ids = [];
            var selectedIds =  domObj.grids.$prdInfoGrd.jqGrid("getGridParam", "selarrrow");

            for (var i = 0;i<selectedIds.length;i++){
                var rowId = selectedIds[i];
                var rowData = domObj.grids.$prdInfoGrd
                    .jqGrid("getRowData", rowId);
                ids.push(rowData.samp_seq_id);
            }
            if(selectedIds.length < 1){
                showErrorDialog("", "请选择一条数据");
                return false;
            }
            var str = '<form id="exportForm" action="exportList.do?samp_seq_id='+ids+'" method="post">';
            str = str + "</form>";

            $(str).appendTo("body");
            $("#exportForm").submit();
        },
        deleteInfo : function () {
            var ids = [];
            var selectedIds =  domObj.grids.$prdInfoGrd.jqGrid("getGridParam", "selarrrow");

            for (var i = 0;i<selectedIds.length;i++){
                var rowId = selectedIds[i];
                var rowData = domObj.grids.$prdInfoGrd
                    .jqGrid("getRowData", rowId);
                ids.push(rowData.samp_seq_id);
            }
            if(selectedIds.length < 1){
                showErrorDialog("", "请选择一条数据");
                return false;
            }
            var inObj,
                outObj,
                iary,
                iary = {
                    samp_seq_ids : ids
                },
                inObj = {
                    trx_id: VAL.T_FSPSMPDEF,
                    action_flg: 'D',
                    iary: [iary]
                };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                showSuccessDialog("删除成功");
            }
        },
        register : function () {
            var inObj,
                outObj,
                iary,
                act_pos_flg,
                iary = {
                    samp_tpl_id : $("#samp_tpl_id_fk").val(),   //模版ID
                    mdl_id_fk : $("#mdl_id_fk").val(),             //产品代码
                    tool_id : $("#tool_id_fk").val(),
                    path_id_fk : $("#path_id_fk").val(),           //工艺路线
                    path_ver_fk : $("#path_ver_fk").val(),        //工艺版本
                    ope_no_fk : $("#ope_no_fk").val(),             //操作代码
                    ope_id_fk     : $("#ope_id_fk").val(),        //制程代码
                    ope_ver_fk     : $("#ope_ver_fk").val(),        //制程代码
                    samp_qty  : $("#samp_qty").val(),                //取样频率
                    rsv_batch_qty : $("#rsv_batch_qty").val(),       //取样批数
                    evt_note : $("#crt_note").val(),
                    evt_usr : VAL.EVT_USER,
                };
            inObj = {
                trx_id: VAL.T_FSPSMPDEF,
                action_flg: 'A',
                iary: [iary],

            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                showSuccessDialog("登记成功");
            }
        },
        priority : function() {
            var rowId = domObj.grids.$prdInfoGrd
                .jqGrid("getGridParam", "selrow");
            var rowData = domObj.grids.$prdInfoGrd
                .jqGrid("getRowData", rowId);
            if(!rowData.hasOwnProperty("samp_seq_id")){
                showErrorDialog("", "请选择一条数据");
                return false;
            }
            var samp_seq_id = rowData.samp_seq_id;
            var inObj,
                outObj,
                iary,
                iary = {
                    samp_seq_id : samp_seq_id
                },
                inObj = {
                    trx_id: VAL.T_FSPSMPDEF,
                    action_flg: 'T',
                    iary: [iary]
                };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                showSuccessDialog("优先级已经提升！");
            }
        },
        importId : function () {
            $("#uploadModel").modal("show");

        },

    };
    var iniContorlData = function(){
        toolFun.initSampTplId();
        toolFun.initMdl();
        //产品改变
        domObj.$mdl_id_fk.change(function () {
            domObj.$path_id_fk.empty();
            SelectDom.addSelect(domObj.$path_id_fk,"","");
            domObj.$path_ver_fk.empty();
            SelectDom.addSelect(domObj.$path_ver_fk,"","");
            domObj.$ope_no_fk.empty();
            SelectDom.addSelect(domObj.$ope_no_fk,"","");
            domObj.$ope_id_fk.empty();
            SelectDom.addSelect(domObj.$ope_id_fk,"","");
            domObj.$ope_ver_fk.empty();
            SelectDom.addSelect(domObj.$ope_ver_fk,"","");
            if (domObj.$mdl_id_fk.val() != ""){
                toolFun.showPath_id();
            } else {
                toolFun.checkPathIdNoRes(null,"MAIN");
            }

        });
        //工艺路线改变
        domObj.$path_id_fk.change(function () {
            domObj.$path_ver_fk.empty();
            SelectDom.addSelect(domObj.$path_ver_fk,"","");
            domObj.$ope_no_fk.empty();
            SelectDom.addSelect(domObj.$ope_no_fk,"","");
            toolFun.initPathVer();
            toolFun.initOpe_no();
        });
        //工艺版本改变
        domObj.$path_id_fk.change(function () {
            domObj.$ope_no_fk.empty();
            SelectDom.addSelect(domObj.$ope_no_fk,"","");
            toolFun.initOpe_no();
        });
        //操作代码改变 制程
        domObj.$ope_no_fk.change(function () {
            domObj.$ope_id_fk.empty();
            SelectDom.addSelect(domObj.$ope_id_fk,"","");
            domObj.$ope_ver_fk.empty();
            SelectDom.addSelect(domObj.$ope_ver_fk,"","");
            domObj.$tool_id.empty();
            SelectDom.addSelect(domObj.$tool_id,"","");
            toolFun.initOpe_id();
            toolFun.initTool();
        });



    };
    var iniButtonAction = function() {
        domObj.buttons.$query_btn.click(function () {
            btnFun.getRelationList();
        });
        domObj.buttons.$acti_free.click(function () {
            btnFun.active();
            btnFun.getRelationList();
        });
        domObj.buttons.$delete_btn.click(function () {
            btnFun.deleteInfo();
            btnFun.getRelationList();
        });
        domObj.buttons.$addRsn_btn.click(function () {
            btnFun.register();
            btnFun.getRelationList();
        });
        domObj.buttons.$priority_btn.click(function () {
            btnFun.priority();
            btnFun.getRelationList();
        });
        //导入模版下载
        domObj.buttons.$importSte_btn.click(function(){
            btnFun.downSample();
        });
        //导出列表
        domObj.buttons.$export_btn.click(function () {
            btnFun.export();
        });
        //导入
        domObj.buttons.$import_btn.click(function () {
            btnFun.importId();
        });

        $("#uploadModel").on("click","#uploadBtn",function(){
            $("#uploadForm").submit();

        })
    };

    var initFun = function fs() {
        initPrdInfoGrd();
        iniContorlData();
        iniButtonAction();

    }();
    //模态框拖动
    $(".modal-content").draggable();

    //表格自适应
    function resizeFnc(){
        var offsetBottom, divWidth;
        divWidth = $("#prdInfoGrdDiv").width();
        offsetBottom = $(Window).height() -  $("#prdInfoGrdDiv").offset().top;
        $("#prdInfoGrdDiv").height(offsetBottom * 0.95);
        $("#prdInfoGrd").setGridWidth(divWidth * 0.99);
        $("#prdInfoGrd").setGridHeight(offsetBottom * 0.99 - 51);
    };
    resizeFnc();
    domObj.$window.resize(function() {
        resizeFnc();
    });















});